﻿<UserControl x:Class="SteveDower.SimpleBacklog.BacklogView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:vsfx="clr-namespace:Microsoft.VisualStudio.Shell;assembly=Microsoft.VisualStudio.Shell.10.0"
             xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
             xmlns:s="clr-namespace:System;assembly=mscorlib"
             xmlns:l="clr-namespace:SteveDower.SimpleBacklog"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="700"
             Name="ctrlBacklogView"
             Background="{DynamicResource {x:Static vsfx:VsBrushes.ToolWindowBackgroundKey}}">
    <Control.Resources>
        <BitmapImage x:Key="Critical" UriSource="Resources/Critical.png" DecodePixelWidth="8" DecodePixelHeight="8" />
        <BitmapImage x:Key="Important" UriSource="Resources/Important.png" DecodePixelWidth="8" DecodePixelHeight="8" />
        <BitmapImage x:Key="Wishlist" UriSource="Resources/Wishlist.png" DecodePixelWidth="8" DecodePixelHeight="8" />
        <BitmapImage x:Key="CriticalBig" UriSource="Resources/Critical Big.png" DecodePixelWidth="32" DecodePixelHeight="32" />
        <BitmapImage x:Key="ImportantBig" UriSource="Resources/Important Big.png" DecodePixelWidth="32" DecodePixelHeight="32" />
        <BitmapImage x:Key="WishlistBig" UriSource="Resources/Wishlist Big.png" DecodePixelWidth="32" DecodePixelHeight="32" />

        <BitmapImage x:Key="Working" UriSource="Resources/Working.png" DecodePixelWidth="16" DecodePixelHeight="16" />
        <BitmapImage x:Key="Deferred" UriSource="Resources/Deferred.png" DecodePixelWidth="16" DecodePixelHeight="16" />
        <BitmapImage x:Key="Testing" UriSource="Resources/Testing.png" DecodePixelWidth="16" DecodePixelHeight="16" />
        <BitmapImage x:Key="Completed" UriSource="Resources/Completed.png" DecodePixelWidth="16" DecodePixelHeight="16" />

        <BitmapImage x:Key="New" UriSource="Resources/New.png" DecodePixelWidth="16" DecodePixelHeight="16" />
        <BitmapImage x:Key="Tools" UriSource="Resources/Tools.png" DecodePixelWidth="16" DecodePixelHeight="16" />
        <BitmapImage x:Key="Import" UriSource="Resources/Import.png" DecodePixelWidth="32" DecodePixelHeight="32" />
        <BitmapImage x:Key="Export" UriSource="Resources/Export.png" DecodePixelWidth="32" DecodePixelHeight="32" />

        <CollectionViewSource x:Key="FilteredItems"
                              Source="{Binding}"
                              Filter="FilteredItems_Filter">
            <CollectionViewSource.SortDescriptions>
                <scm:SortDescription PropertyName="Status" />
                <scm:SortDescription PropertyName="Priority" />
                <scm:SortDescription PropertyName="Category" />
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>

        <ContextMenu x:Key="ItemMenu">
            <MenuItem Command="{x:Static l:BacklogView.SetStatusUnstarted}" CommandParameter="{Binding}"
                      Header="_Unstarted" />
            <MenuItem Command="{x:Static l:BacklogView.SetStatusWorking}" CommandParameter="{Binding}"
                      Header="_Working">
                <MenuItem.Icon>
                    <Image Source="{StaticResource Working}" Width="16" Height="16"/>
                </MenuItem.Icon>
            </MenuItem>
            <MenuItem Command="{x:Static l:BacklogView.SetStatusDeferred}" CommandParameter="{Binding}"
                      Header="_Deferred">
                <MenuItem.Icon>
                    <Image Source="{StaticResource Deferred}" Width="16" Height="16"/>
                </MenuItem.Icon>
            </MenuItem>
            <MenuItem Command="{x:Static l:BacklogView.SetStatusTesting}" CommandParameter="{Binding}"
                      Header="_Testing">
                <MenuItem.Icon>
                    <Image Source="{StaticResource Testing}" Width="16" Height="16"/>
                </MenuItem.Icon>
            </MenuItem>
            <MenuItem Command="{x:Static l:BacklogView.SetStatusCompleted}" CommandParameter="{Binding}"
                      Header="_Completed">
                <MenuItem.Icon>
                    <Image Source="{StaticResource Completed}" Width="16" Height="16"/>
                </MenuItem.Icon>
            </MenuItem>

            <Separator />

            <MenuItem Command="{x:Static l:BacklogView.SetPriorityWishlist}" CommandParameter="{Binding}"
                      Header="Wishlist">
                <MenuItem.Icon>
                    <Image Source="{StaticResource Wishlist}" Width="8" Height="8" />
                </MenuItem.Icon>
            </MenuItem>
            <MenuItem Command="{x:Static l:BacklogView.SetPriorityImportant}" CommandParameter="{Binding}"
                      Header="Important">
                <MenuItem.Icon>
                    <Image Source="{StaticResource Important}" Width="8" Height="8" />
                </MenuItem.Icon>
            </MenuItem>
            <MenuItem Command="{x:Static l:BacklogView.SetPriorityCritical}" CommandParameter="{Binding}"
                      Header="Critical">
                <MenuItem.Icon>
                    <Image Source="{StaticResource Critical}" Width="8" Height="8" />
                </MenuItem.Icon>
            </MenuItem>
            
            <Separator />
            
            <MenuItem Command="{x:Static l:BacklogView.DeleteItem}" CommandParameter="{Binding}"
                      Header="Delete" />
        </ContextMenu>

        <DataTemplate DataType="{x:Type l:BacklogItem}">
            <Canvas ContextMenu="{DynamicResource ItemMenu}" Width="16" Height="16"
                    MouseLeftButtonDown="Item_MouseLeftButtonDown" Tag="{Binding}" >
                <Image Canvas.Left="0" Canvas.Top="0" Width="16" Height="16" x:Name="StatusImage" />
                <Image Canvas.Left="8" Canvas.Top="8" Width="8" Height="8" x:Name="PriorityImage" />
            </Canvas>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=Status}" Value="{x:Static l:Status.Unstarted}">
                    <Setter TargetName="PriorityImage" Property="Canvas.Left" Value="4" />
                    <Setter TargetName="PriorityImage" Property="Canvas.Top" Value="4" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=Status}" Value="{x:Static l:Status.Working}">
                    <Setter TargetName="StatusImage" Property="Source" Value="{StaticResource Working}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=Status}" Value="{x:Static l:Status.Deferred}">
                    <Setter TargetName="StatusImage" Property="Source" Value="{StaticResource Deferred}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=Status}" Value="{x:Static l:Status.Testing}">
                    <Setter TargetName="StatusImage" Property="Source" Value="{StaticResource Testing}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=Status}" Value="{x:Static l:Status.Completed}">
                    <Setter TargetName="StatusImage" Property="Source" Value="{StaticResource Completed}" />
                </DataTrigger>

                <DataTrigger Binding="{Binding Path=Priority}" Value="{x:Static l:Priority.Wishlist}">
                    <Setter TargetName="PriorityImage" Property="Source" Value="{StaticResource Wishlist}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=Priority}" Value="{x:Static l:Priority.Important}">
                    <Setter TargetName="PriorityImage" Property="Source" Value="{StaticResource Important}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=Priority}" Value="{x:Static l:Priority.Critical}">
                    <Setter TargetName="PriorityImage" Property="Source" Value="{StaticResource Critical}" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>

        <l:StringToVisibilityConverter x:Key="IfNotEmpty" />
        <Style TargetType="TextBox" x:Key="GridTextBoxStyle">
            <Setter Property="BorderBrush" Value="Transparent"/>
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="Background" Value="Transparent"/>
        </Style>
        <DataTemplate x:Key="EditTextTemplate">
            <Grid>
                <TextBlock x:Name="lbl"
                           Text="Text"
                           FontStyle="Italic" Foreground="Gray"
                           Visibility="Hidden"/>
                <TextBox x:Name="txt" MinWidth="{Binding ElementName=lbl,Path=Width}"
                         Text="{Binding Path=Text,UpdateSourceTrigger=PropertyChanged}"
                         Style="{StaticResource GridTextBoxStyle}"
                         Keyboard.GotKeyboardFocus="txt_GotKeyboardFocus">
                </TextBox>
            </Grid>
            <DataTemplate.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding ElementName=txt,Path=Text}" Value="" />
                        <Condition Binding="{Binding ElementName=txt,Path=IsKeyboardFocused}" Value="False" />
                    </MultiDataTrigger.Conditions>
                    <MultiDataTrigger.Setters>
                        <Setter TargetName="lbl" Property="Visibility" Value="{x:Static Visibility.Visible}" />
                    </MultiDataTrigger.Setters>
                </MultiDataTrigger>
                <DataTrigger Binding="{Binding Path=Status}" Value="{x:Static l:Status.Completed}">
                    <Setter TargetName="txt" Property="Foreground" Value="Gray" />
                    <Setter TargetName="txt" Property="TextDecorations" Value="Strikethrough" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
        <DataTemplate x:Key="EditCategoryTemplate">
            <Grid>
                <TextBlock x:Name="lbl"
                           Text="Category"
                           FontStyle="Italic" Foreground="Gray"
                           Visibility="Hidden"/>
                <TextBox x:Name="txt" MinWidth="{Binding ElementName=lbl,Path=Width}"
                         Text="{Binding Path=Category,UpdateSourceTrigger=PropertyChanged}"
                         Style="{StaticResource GridTextBoxStyle}"
                         Keyboard.GotKeyboardFocus="txt_GotKeyboardFocus"/>
            </Grid>
            <DataTemplate.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding ElementName=txt,Path=Text}" Value="" />
                        <Condition Binding="{Binding ElementName=txt,Path=IsKeyboardFocused}" Value="False" />
                    </MultiDataTrigger.Conditions>
                    <MultiDataTrigger.Setters>
                        <Setter TargetName="lbl" Property="Visibility" Value="{x:Static Visibility.Visible}" />
                    </MultiDataTrigger.Setters>
                </MultiDataTrigger>
                <DataTrigger Binding="{Binding Path=Status}" Value="{x:Static l:Status.Completed}">
                    <Setter TargetName="txt" Property="Foreground" Value="Gray" />
                    <Setter TargetName="txt" Property="TextDecorations" Value="Strikethrough" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
        <DataTemplate x:Key="EditNotesTemplate">
            <Grid>
                <TextBlock x:Name="lbl"
                           Text="Notes"
                           FontStyle="Italic" Foreground="Gray"
                           Visibility="Hidden"/>
                <TextBox x:Name="txt" MinWidth="{Binding ElementName=lbl,Path=Width}"
                         Text="{Binding Path=Notes,UpdateSourceTrigger=PropertyChanged}"
                         Style="{StaticResource GridTextBoxStyle}"
                         Keyboard.GotKeyboardFocus="txt_GotKeyboardFocus"/>
            </Grid>
            <DataTemplate.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding ElementName=txt,Path=Text}" Value="" />
                        <Condition Binding="{Binding ElementName=txt,Path=IsKeyboardFocused}" Value="False" />
                    </MultiDataTrigger.Conditions>
                    <MultiDataTrigger.Setters>
                        <Setter TargetName="lbl" Property="Visibility" Value="{x:Static Visibility.Visible}" />
                    </MultiDataTrigger.Setters>
                </MultiDataTrigger>
                <DataTrigger Binding="{Binding Path=Status}" Value="{x:Static l:Status.Completed}">
                    <Setter TargetName="txt" Property="Foreground" Value="Gray" />
                    <Setter TargetName="txt" Property="TextDecorations" Value="Strikethrough" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>

    </Control.Resources>
    <Grid>
        <DockPanel>
            <ToolBar DockPanel.Dock="Top" Height="32">
                <Label VerticalAlignment="Center">_Show</Label>
                <ComboBox x:Name="lstStatusFilter"
                          IsEditable="False"
                          Width="150"
                          SelectedIndex="0"
                          SelectionChanged="FilteredItems_Invalidate">
                    <ComboBoxItem>All items</ComboBoxItem>
                    <ComboBoxItem>Active items</ComboBoxItem>
                    <ComboBoxItem>Unstarted items</ComboBoxItem>
                    <ComboBoxItem>Completed items</ComboBoxItem>
                </ComboBox>

                <Separator/>

                <l:DropDownButton x:Name="btnNew"
                                  Style="{DynamicResource {x:Static ToolBar.CheckBoxStyleKey}}">
                    <StackPanel Orientation="Horizontal" Height="16">
                        <Image Source="{StaticResource New}" />
                        <Label Padding="0">New</Label>
                        <Canvas Width="12" Height="16">
                            <Polygon Fill="Black">
                                <Polygon.Points>
                                    <Point X="3" Y="7" />
                                    <Point X="9" Y="7" />
                                    <Point X="6" Y="10" />
                                </Polygon.Points>
                            </Polygon>
                        </Canvas>
                    </StackPanel>
                    <l:DropDownButton.DropDown>
                        <ContextMenu IsOpen="{Binding ElementName=btnNew,Path=IsChecked}"
                                     Placement="Bottom" PlacementTarget="{Binding ElementName=btnNew}">
                            <ContextMenu.Template>
                                <ControlTemplate>
                                    <Border Background="{TemplateBinding Background}"
                                            BorderBrush="{TemplateBinding BorderBrush}"
                                            BorderThickness="{TemplateBinding BorderThickness}">
                                        <Grid>
                                            <Rectangle Fill="#F1F1F1"
                                                       HorizontalAlignment="Left"
                                                       Width="38"
                                                       Margin="2"
                                                       RadiusX="2"
                                                       RadiusY="2"/>
                                            <Rectangle HorizontalAlignment="Left"
                                                       Width="1"
                                                       Margin="40,2,0,2"
                                                       Fill="#E2E3E3"/>
                                            <Rectangle HorizontalAlignment="Left"
                                                       Width="1"
                                                       Margin="41,2,0,2"
                                                       Fill="White"/>

                                            <ScrollViewer CanContentScroll="true"
                                              Grid.ColumnSpan="2" Margin="1,0"
                                              Style="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type FrameworkElement}, ResourceId=MenuScrollViewer}}">
                                                <ItemsPresenter Margin="{TemplateBinding Padding}"
                                                    KeyboardNavigation.DirectionalNavigation="Cycle"
                                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                            </ScrollViewer>
                                        </Grid>
                                    </Border>
                                </ControlTemplate>
                            </ContextMenu.Template>
                            <MenuItem Click="NewWishlistItem">
                                <MenuItem.Header>
                                    <Label Content="_Wishlist"/>
                                </MenuItem.Header>
                                <MenuItem.Icon>
                                    <Image Source="{StaticResource WishlistBig}" Width="32" Height="32" />
                                </MenuItem.Icon>
                            </MenuItem>
                            <MenuItem Click="NewImportantItem">
                                <MenuItem.Header>
                                    <Label Content="_Important"/>
                                </MenuItem.Header>
                                <MenuItem.Icon>
                                    <Image Source="{StaticResource ImportantBig}" Width="32" Height="32" />
                                </MenuItem.Icon>
                            </MenuItem>
                            <MenuItem Click="NewCriticalItem">
                                <MenuItem.Header>
                                    <Label Content="_Critical"/>
                                </MenuItem.Header>
                                <MenuItem.Icon>
                                    <Image Source="{StaticResource CriticalBig}" Width="32" Height="32" />
                                </MenuItem.Icon>
                            </MenuItem>
                        </ContextMenu>
                    </l:DropDownButton.DropDown>
                </l:DropDownButton>

                <Separator/>

                <CheckBox IsChecked="True" x:Name="chkShowCritical"
                          Checked="FilteredItems_Invalidate" Unchecked="FilteredItems_Invalidate">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="{StaticResource Critical}" Margin="4" Width="8" Height="8" VerticalAlignment="Center" />
                        <TextBlock>Critical</TextBlock>
                    </StackPanel>
                </CheckBox>
                <CheckBox IsChecked="True" x:Name="chkShowImportant"
                          Checked="FilteredItems_Invalidate" Unchecked="FilteredItems_Invalidate">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="{StaticResource Important}" Margin="4" Width="8" Height="8" VerticalAlignment="Center" />
                        <TextBlock>Important</TextBlock>
                    </StackPanel>
                </CheckBox>
                <CheckBox IsChecked="True" x:Name="chkShowWishlist"
                          Checked="FilteredItems_Invalidate" Unchecked="FilteredItems_Invalidate">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="{StaticResource Wishlist}" Margin="4" Width="8" Height="8" VerticalAlignment="Center" />
                        <TextBlock>Wishlist</TextBlock>
                    </StackPanel>
                </CheckBox>

                <Separator/>

                <l:DropDownButton Style="{DynamicResource {x:Static ToolBar.CheckBoxStyleKey}}">
                    <StackPanel Orientation="Horizontal" Height="16">
                        <Image Source="{StaticResource Tools}" />
                        <Canvas Width="12" Height="16">
                            <Polygon Fill="Black">
                                <Polygon.Points>
                                    <Point X="3" Y="7" />
                                    <Point X="9" Y="7" />
                                    <Point X="6" Y="10" />
                                </Polygon.Points>
                            </Polygon>
                        </Canvas>
                    </StackPanel>
                    <l:DropDownButton.DropDown>
                        <ContextMenu>
                            <ContextMenu.Template>
                                <ControlTemplate>
                                    <Border Background="{TemplateBinding Background}"
                                            BorderBrush="{TemplateBinding BorderBrush}"
                                            BorderThickness="{TemplateBinding BorderThickness}">
                                        <Grid>
                                            <Rectangle Fill="#F1F1F1"
                                                       HorizontalAlignment="Left"
                                                       Width="38"
                                                       Margin="2"
                                                       RadiusX="2"
                                                       RadiusY="2"/>
                                            <Rectangle HorizontalAlignment="Left"
                                                       Width="1"
                                                       Margin="40,2,0,2"
                                                       Fill="#E2E3E3"/>
                                            <Rectangle HorizontalAlignment="Left"
                                                       Width="1"
                                                       Margin="41,2,0,2"
                                                       Fill="White"/>

                                            <ScrollViewer CanContentScroll="true"
                                              Grid.ColumnSpan="2" Margin="1,0"
                                              Style="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type FrameworkElement}, ResourceId=MenuScrollViewer}}">
                                                <ItemsPresenter Margin="{TemplateBinding Padding}"
                                                    KeyboardNavigation.DirectionalNavigation="Cycle"
                                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                            </ScrollViewer>
                                        </Grid>
                                    </Border>
                                </ControlTemplate>
                            </ContextMenu.Template>
                            <MenuItem>
                                <MenuItem.Header>
                                    <Label Content="_Import Backlog"/>
                                </MenuItem.Header>
                                <MenuItem.Icon>
                                    <Image Source="{StaticResource Import}" Width="32" Height="32" />
                                </MenuItem.Icon>
                            </MenuItem>
                            <MenuItem>
                                <MenuItem.Header>
                                    <Label Content="_Export Backlog"/>
                                </MenuItem.Header>
                                <MenuItem.Icon>
                                    <Image Source="{StaticResource Export}" Width="32" Height="32" />
                                </MenuItem.Icon>
                            </MenuItem>
                        </ContextMenu>
                    </l:DropDownButton.DropDown>
                </l:DropDownButton>
            </ToolBar>

            <ListView ItemsSource="{Binding Source={StaticResource FilteredItems}}">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header=""
                                        Width="24"/>
                        <GridViewColumn Header="Item"
                                        Width="120"
                                        CellTemplate="{StaticResource EditTextTemplate}" />
                        <GridViewColumn Header="Category"
                                        Width="100"
                                        CellTemplate="{StaticResource EditCategoryTemplate}" />
                        <GridViewColumn Header="Notes"
                                        Width="auto"
                                        CellTemplate="{StaticResource EditNotesTemplate}" />
                    </GridView>
                </ListView.View>
            </ListView>
        </DockPanel>
    </Grid>
</UserControl>
